import { createRouter, createWebHistory } from 'vue-router';
const routes = [
    {
        path: '/',
        name: 'home',
        menubar: 'home',
        meta: {
            title: '首页',
            auth: true,
        },
        component: () => import('@/pages/Home.vue'),
    },
    {
        path: '/console',
        name: 'console',
        menubar: 'console',
        meta: {
            title: '控制台',
            auth: true,
        },
        component: () => import('@/pages/Console.vue'),
        children: []
    },
    {
        path: '/helper',
        name: 'helper',
        menubar: 'helper',
        meta: {
            title: '帮助',
            auth: true,
        },
        component: () => import('@/pages/Helper.vue'),
        children: [
            {
                path:'about',
                name: 'about',
                component: () => import('@/pages/helper/About.vue'),
                meta: {
                    title: '关于',
                }
            },
            {
                path:'docs',
                name: 'docs',
                component: () => import('@/pages/helper/Docs.vue'),
                meta: {
                    title: '文档',
                }
            },
        ]
    },
    {
        path: '/account',
        name: 'account',
        menubar: 'account',
        meta: {
            title: '账号',
            auth: false,
        },
        component: () => import('@/pages/Account.vue'),
        redirect: '/account/login',
        children: [
            {
                path:'login',
                name: 'login',
                component: () => import('@/pages/account/Login.vue'),
                meta: {
                    title: '登录',
                }
            },
            {
                path:'register',
                name: 'register',
                component: () => import('@/pages/account/Register.vue'),
                meta: {
                    title: '注册',
                }
            },
            {
                path:'forget',
                name: 'forget',
                component: () => import('@/pages/account/Forget.vue'),
                meta: {
                    title: '重置密码',
                }
            }
        ]
    },
    {
        // 匹配所有路径，匹配到未声明的路由路径时展示404
        path: "/:w+",
        name: "*",
        menubar: '404',
        meta: {
            title: '404',
            auth: false,
        },
        component: () => import("@/pages/Notfound.vue")
    },
];

const AppRouter = createRouter({
    history: createWebHistory(),
    routes,
    strict: true,
});

/**
 * 用户权限控制
 *  添加路由守卫,每次跳转前确认用户权限
 *  防止手动输入目标路由时,绕过绕过权限检测
 * 路由守卫:
 * 验证用户登录状态
 *  1.验证是否登录
 *  2.验证当前用户是否有权限访问目标路由地址
 */
AppRouter.beforeEach((to, from, next) => {
    //获取用户token
    let isLogin = sessionStorage.getItem('TOKEN')
    // if (isLogin || to.path === '/account/login' || to.path === '/help' || to.path === '') {
    //     next()
    // }
    // else {
    //     next('/account/login')
    // }
    next()
});
export default AppRouter;
